﻿
var xHRObject = false;

if (window.ActiveXObject)
{
xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xHRObject = new XMLHttpRequest();
}
  
    function getData()
    {
        //Check to see if the XMlHttpRequest object is ready and whether it has  
        //returned a legitmate response
        if (xHRObject.readyState == 4)
        {
        if (xHRObject.status == 200)
        {           
       
            if (window.ActiveXObject)
            {
                // Load XML 
                var xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xml.load("Stocks.xml");
       
                //Load XSL
                var xsl = new ActiveXObject("Microsoft.XMLDOM");
                xsl.async = false;
                xsl.load("Stocks.xsl");
       
                //Transform
                var transform = xml.transformNode(xsl);
                var Stocks = document.getElementById("Stocks");
            }
            else
            {
                var xsltProcessor = new XSLTProcessor();
              
                //Load XSL
                var XHRObject = new XMLHttpRequest();
                XHRObject.open("GET", "Stocks.xsl", false);
                XHRObject.send(null);

                xslStylesheet = XHRObject.responseXML;
                xsltProcessor.importStylesheet(xslStylesheet);

                //Load XML
                xmlDoc = XHRObject.responseXML;
                
                //Transform
                var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
                document.getElementById("Stocks").innerHTML = "";
                document.getElementById("Stocks").appendChild(fragment);
         
            }

            Stocks.innerHTML = transform; 
            
            //Clear the object and call the getDocument function in 10 seconds
            xHRObject.abort();
            setTimeout("getDocument()", 5000);     
        }  
        else
        {
            Stocks = document.getElementById("Stocks");
            if (Stocks.innerHTML.indexOf("available")==-1)
            {
                Stocks.innerHTML += "<br/><span>Current stock information not available, currently showing the last available data</span>";            
            }
            xHRObject.abort();
            setTimeout("getDocument()", 5000);    
        }
        }
    }      
    
    function getDocument()
    {
     //Reset the function
     xHRObject.onreadystatechange = getData;
     num = (Math.round((Math.random()*999999)+1))
     xHRObject.open("GET", "GetStocksList.aspx?id="+num,true);
     xHRObject.send(null);
     }
